<?php
    
        $width = $_POST['width'];
        $height = $_POST['height'];
        $required = ($width*$height)/2;
        $handle = opendir('images');
        
        $images = Array();
        
        while (false !== ($entry = readdir($handle)) && $required > 0) {
             if ($entry != "." && $entry != "..") {
                array_push($images, $entry);
                $required--;
             }
        }
        
        shuffle($images);
        
        //duplicate array
        $imagesFinal = Array();
        foreach($images as $image){
           array_push($imagesFinal, $image);
           array_push($imagesFinal, $image);
            
        }
        shuffle($imagesFinal);
        
        header('Content-type: application/json');
        
        echo "{";
        echo "\"memory\": {";
    
        echo "\"urlprefix\" : \"images/\",";
        echo "\"tiles\": [";
        
        $counter  = 1;
        foreach($imagesFinal as $img){
            if ($counter > 1)
                echo ",";
            
            echo "{";
            echo "        \"label\": \"l$counter\",";
            echo "        \"url\": \"images/$img\"";
            echo "    }";
            
            $counter++;
        }
        
        
        echo " ] }}";
    
?>